/**********************************************
** @Des: This file ...
** @Author: xiaomeng79
** @Date:   17-11-17 上午9:57
***********************************************/
package controllers

import (
	"time"
	"github.com/xiaomeng79/go-rbac/models"
	"github.com/xiaomeng79/go-rbac/libs"
)

type PrivilegeController struct {
	BaseController
}

/**
添加
 */
func (this *PrivilegeController) Add() {
	Privilege := new(models.Privilege)
	if this.isPost() {//post请求
		Privilege.Pri_name = libs.FilterStr(this.GetString("pri_name"))
		Privilege.Controller_name = libs.FilterStr(this.GetString("controller_name"))
		Privilege.Action_name = libs.FilterStr(this.GetString("action_name"))
		Privilege.Par_path = libs.FilterStr(this.GetString("par_path"))
		Privilege.Created_at = time.Now().Unix()
		Privilege.Updated_at = time.Now().Unix()
		Privilege.Is_usable = 1
		if _,err := Privilege.Add();err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
			this.ajaxMsg("添加成功",MSG_OK)

	} else {
		result := Privilege.Lst()
		this.Data["Data"] = result
		this.Data["Title"] = "添加权限"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "权限列表"
		this.Data["Currentname"] = "添加权限"
		this.display()
	}
}

/**
列表
 */
func (this *PrivilegeController) Lst() {
	Privilege := new(models.Privilege)
	result := Privilege.Lst()
	this.Data["Title"] = "权限列表"
	this.Data["Navurl"] = "lst"
	this.Data["Navname"] = "权限列表"
	this.Data["Currentname"] = "权限列表"
	this.Data["Data"] = result
	this.display()
}

/**
修改
 */
func (this *PrivilegeController) Edit() {
	if this.isPost() {//post请求
		Privilege := new(models.Privilege)
		Privilege.Id,_ = this.GetInt("id")
		Privilege.Pri_name = libs.FilterStr(this.GetString("pri_name"))
		Privilege.Controller_name = libs.FilterStr(this.GetString("controller_name"))
		Privilege.Action_name = libs.FilterStr(this.GetString("action_name"))
		Privilege.Par_path = libs.FilterStr(this.GetString("par_path"))
		Privilege.Updated_at = time.Now().Unix()
		if err := Privilege.Edit("pri_name","controller_name","action_name","par_path","updated_at");err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
		this.ajaxMsg("修改成功",MSG_OK)

	} else {
		Privilege_id,_ := this.GetInt("id")
		Privilege,err := models.GetPrivilegeById(Privilege_id)
		if err != nil {
			this.redirect(this.URLFor("PrivilegeController.Lst"))
		}
		result := Privilege.Lst()
		this.Data["Pardata"] = result
		this.Data["Data"] = Privilege
		this.Data["Title"] = "修改权限"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "权限列表"
		this.Data["Currentname"] = "修改权限"
		this.display()
	}
}

/**
删除
 */
func (this *PrivilegeController) Del() {
		Id, _ := this.GetInt("id")

		Privilege,err := models.GetPrivilegeById(Id)
		if err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}
		Privilege.Updated_at = time.Now().Unix()
		Privilege.Is_usable = 0
		//软删除
		if err := Privilege.Edit(); err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}

		this.ajaxMsg("删除成功", MSG_OK)
}